<template>
  <div class="fullscreen-login">
    <!-- 左侧品牌展示区域 -->
    <div class="login-brand-section">
      <div class="brand-content">
        <div class="brand-logo">
          <img src="/vite.svg" alt="Logo" class="logo-img" />
          <h1>Bit Event</h1>
        </div>
        <h2 class="brand-title">打造精彩活动体验</h2>
        <p class="brand-description">
          一站式活动管理平台，为您提供专业、高效的活动策划与执行解决方案
        </p>
        <div class="feature-list">
          <div class="feature-item">
            <i class="feature-icon">✓</i>
            <span>智能活动策划</span>
          </div>
          <div class="feature-item">
            <i class="feature-icon">✓</i>
            <span>实时数据分析</span>
          </div>
          <div class="feature-item">
            <i class="feature-icon">✓</i>
            <span>高效团队协作</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单区域 -->
    <div class="login-form-section">
      <div class="form-container">
        <!-- 登录标题 -->
        <div class="form-header">
          <h2>欢迎回来</h2>
          <p class="subtitle">请登录您的账户继续访问</p>
        </div>

        <!-- 登录表单（修复组件用法） -->
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="loginFormRef"
          class="auth-form"
          label-width="0px" 
        >
          <!-- 用户名输入框（修复验证触发） -->
          <el-form-item prop="username" class="form-item-custom">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
            />
          </el-form-item>

          <!-- 密码输入框（修复验证触发） -->
          <el-form-item prop="password" class="form-item-custom">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
            />
          </el-form-item>

          <!-- 记住我和忘记密码 -->
          <div class="login-options">
            <el-checkbox v-model="loginForm.rememberMe" class="remember-checkbox">
              记住我
            </el-checkbox>
            <el-link type="primary" :underline="false" @click="handleForgotPassword">
              忘记密码？
            </el-link>
          </div>

          <!-- 登录按钮 -->
          <el-form-item class="form-item-custom">
            <el-button
              type="primary"
              class="login-btn"
              :loading="loading"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>

          <!-- 其他登录方式（添加交互） -->
          <div class="alternative-login">
            <div class="divider">
              <span>或使用以下方式登录</span>
            </div>
            <div class="social-buttons">
              <el-button 
                type="default" 
                size="large" 
                class="social-btn"
                @click="handleSocialLogin('phone')"
              >
                <i class="social-icon">📱</i>
                <span>手机号</span>
              </el-button>
              <el-button 
                type="default" 
                size="large" 
                class="social-btn"
                @click="handleSocialLogin('wechat')"
              >
                <i class="social-icon">💬</i>
                <span>微信</span>
              </el-button>
              <el-button 
                type="default" 
                size="large" 
                class="social-btn"
                @click="handleSocialLogin('qq')"
              >
                <i class="social-icon">🐧</i>
                <span>QQ</span>
              </el-button>
            </div>
          </div>
        </el-form>
        
        <div class="form-footer">
          <p>还没有账号？ <el-link type="primary" :underline="false">立即注册</el-link></p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/user.js'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    const loginFormRef = ref()
    const loading = ref(false)

    // 登录表单数据
    const loginForm = reactive({
      username: '',
      password: '',
      rememberMe: false
    })

    // 表单验证规则（修复触发时机）
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: ['blur', 'input'] },  // 实时验证
        { min: 3, max: 20, message: '用户名长度在3到20个字符之间', trigger: ['blur', 'input'] }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: ['blur', 'input'] },  // 实时验证
        { min: 6, message: '密码长度至少为6个字符', trigger: ['blur', 'input'] }
      ]
    }

    // 从localStorage读取记住的用户信息（修复密码安全问题）
    const loadRememberedUser = () => {
      const rememberedUser = localStorage.getItem('rememberedUser')
      if (rememberedUser) {
        try {
          const userInfo = JSON.parse(rememberedUser)
          loginForm.username = userInfo.username || ''  // 只回显用户名
          loginForm.rememberMe = true
        } catch (error) {
          console.error('读取记住的用户信息失败:', error)
        }
      }
    }

    // 处理登录
    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      try {
        await loginFormRef.value.validate()
        loading.value = true

        // 使用store进行登录
        await userStore.login({
          username: loginForm.username,
          password: loginForm.password,
          rememberMe: loginForm.rememberMe
        })

        ElMessage.success('登录成功')
        
        // 获取重定向路径，如果没有则跳转到首页
        const redirectPath = router.currentRoute.value.query.redirect || '/dashboard'
        router.push(redirectPath)
      } catch (error) {
        // 错误提示优化
        if (error.response?.data?.message) {
          ElMessage.error(error.response.data.message)  // 显示后端具体错误
        } else if (error.message) {
          ElMessage.error(error.message)
        } else {
          ElMessage.error('登录失败，请重试')
        }
      } finally {
        loading.value = false
      }
    }

    // 处理忘记密码
    const handleForgotPassword = () => {
      router.push('/forgot-password')
    }

    // 处理社交登录
    const handleSocialLogin = (type) => {
      const typeMap = {
        phone: '手机号',
        wechat: '微信',
        qq: 'QQ'
      }
      ElMessage.info(`${typeMap[type]}登录功能暂未开放`)
    }

    // 组件挂载时加载用户信息
    onMounted(() => {
      loadRememberedUser()
    })

    return {
      loginFormRef,
      loading,
      loginForm,
      rules,
      handleLogin,
      handleForgotPassword,
      handleSocialLogin,
      userStore
    }
  }
}
</script>

<style scoped>
/* 基础样式保持不变，补充优化部分 */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

.fullscreen-login {
  display: flex;
  min-height: 100vh;
  overflow: hidden;
}

/* 左侧品牌区域样式保持不变 */
.login-brand-section {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 60px;
  position: relative;
  overflow: hidden;
  border-radius: 0 14px 14px 0;
}

/* 品牌区域动画和装饰保持不变 */
.login-brand-section::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
  animation: float 30s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translate(0, 0) rotate(0deg); }
  25% { transform: translate(10%, 10%) rotate(5deg); }
  50% { transform: translate(0, 20%) rotate(0deg); }
  75% { transform: translate(-10%, 10%) rotate(-5deg); }
}

.brand-content {
  position: relative;
  z-index: 1;
  max-width: 500px;
  animation: slideInLeft 0.8s ease-out forwards;
}

.brand-logo {
  display: flex;
  align-items: center;
  margin-bottom: 40px;
}

.brand-logo .logo-img {
  width: 64px;
  height: 64px;
  margin-right: 16px;
  background: white;
  border-radius: 16px;
  padding: 8px;
}

.brand-logo h1 {
  font-size: 32px;
  font-weight: 700;
  margin: 0;
  background: linear-gradient(135deg, #ffffff 0%, #e0e0ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.brand-title {
  font-size: 42px;
  font-weight: 800;
  margin-bottom: 20px;
  line-height: 1.2;
}

.brand-description {
  font-size: 18px;
  line-height: 1.6;
  opacity: 0.9;
  margin-bottom: 40px;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.feature-item {
  display: flex;
  align-items: center;
  font-size: 16px;
  opacity: 0.9;
  transition: transform 0.2s ease;
}

.feature-item:hover {
  transform: translateX(5px);
  opacity: 1;
}

.feature-icon {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  font-weight: bold;
}

/* 右侧登录表单区域 */
.login-form-section {
  width: 100%;
  max-width: 500px;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.form-container {
  width: 100%;
  max-width: 360px;
}

.form-header {
  margin-bottom: 40px;
  animation: fadeInUp 0.6s ease-out forwards;
  animation-delay: 0.2s;
}

.form-header h2 {
  font-size: 28px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 8px;
}

.form-header .subtitle {
  font-size: 16px;
  color: #606266;
  margin: 0;
}

.auth-form {
  width: 100%;
  animation: fadeInUp 0.6s ease-out forwards;
  animation-delay: 0.4s;
}

/* 修复表单项间距问题（提高优先级） */
.form-item-custom {
  margin-bottom: 24px !important;
}

:deep(.el-input__wrapper) {
  border-radius: 12px !important;
  background-color: #f8fafc;
  transition: all 0.3s ease;
  overflow: hidden;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
  border-color: #667eea;
}

:deep(.el-input__inner) {
  height: 48px;
  font-size: 16px;
  background-color: transparent;
}

:deep(.el-input__prefix) {
  left: 16px;
  color: #909399;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

:deep(.el-checkbox__label) {
  font-size: 14px;
  color: #606266;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #667eea;
  border-color: #667eea;
}

.login-btn {
  width: 100%;
  height: 48px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.login-btn:hover {
  opacity: 0.95;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

.login-btn:active {
  transform: translateY(0);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.login-btn.is-loading {
  opacity: 0.8;
  transform: none;
}

/* 其他登录方式（优化小屏幕显示） */
.alternative-login {
  margin-top: 32px;
  animation: fadeInUp 0.6s ease-out forwards;
  animation-delay: 0.6s;
}

.divider {
  position: relative;
  text-align: center;
  margin-bottom: 24px;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 42%;
  height: 1px;
  background-color: #e0e0e0;
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.divider span {
  color: #909399;
  font-size: 14px;
  background-color: #fff;
  padding: 0 16px;
  position: relative;
  z-index: 1;
}

.social-buttons {
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

/* 修复社交按钮小屏幕挤压问题 */
.social-btn {
  flex: 1;
  min-width: 80px;  /* 增加最小宽度 */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  height: 80px;
  transition: all 0.3s ease;
  border: 1px solid #ebeef5;
  background-color: #fafafa;
}

.social-btn:hover {
  border-color: #667eea;
  background-color: #f5f6ff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.social-icon {
  font-size: 24px;
  margin-bottom: 8px;
}

/* 表单底部 */
.form-footer {
  text-align: center;
  margin-top: 40px;
  font-size: 14px;
  color: #606266;
  animation: fadeInUp 0.6s ease-out forwards;
  animation-delay: 0.8s;
}

:deep(.el-link) {
  font-weight: 500;
  color: #667eea;
}

:deep(.el-link:hover) {
  color: #525fdb;
}

/* 响应式优化（修复小屏幕布局） */
@media (max-width: 1024px) {
  .login-brand-section {
    padding: 0 40px;
  }
  
  .brand-title {
    font-size: 36px;
  }
}

@media (max-width: 768px) {
  .fullscreen-login {
    flex-direction: column;
  }
  
  .login-brand-section {
    width: 100%;
    padding: 40px;
    min-height: 40vh;
  }
  
  .login-form-section {
    width: 100%;
    max-width: 100%;
    padding: 40px 24px;
  }
  
  .brand-logo {
    margin-bottom: 24px;
  }
  
  .brand-title {
    font-size: 32px;
  }
  
  .brand-description {
    font-size: 16px;
    margin-bottom: 24px;
  }
  
  .feature-list {
    display: none;
  }
}

@media (max-width: 480px) {
  .login-brand-section {
    padding: 30px 24px;
    min-height: 200px;  /* 固定最小高度，避免挤压 */
  }
  
  .login-form-section {
    padding: 30px 20px;
  }
  
  .brand-logo .logo-img {
    width: 50px;
    height: 50px;
  }
  
  .brand-logo h1 {
    font-size: 24px;
  }
  
  .brand-title {
    font-size: 28px;
  }
  
  .social-buttons {
    flex-direction: column;
  }
  
  .social-btn {
    flex-direction: row;
    height: 56px;
    justify-content: center;
  }
  
  .social-icon {
    margin-bottom: 0;
    margin-right: 12px;
  }
}

/* 动画效果保持不变 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-50px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
</style>